import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {formatAmount, conventEnumValueToString, formatDateTime} from '../utils';
import {packingListTypeAdd, currency} from '../Enum';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealerCode',
                    defaultMessage: '总代ERP号'
                }),
                dataIndex: 'dealerCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                // render: (text, record) => `(${record.dealerCode})${record.dealerName}`
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partSaleOrderCode',
                    defaultMessage: '备件订单号'
                }),
                dataIndex: 'partSaleOrderCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'partSaleOrderCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.pickingCode',
                    defaultMessage: '备料单号'
                }),
                dataIndex: 'pickingCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'pickingCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.palletCode',
                    defaultMessage: '托盘号'
                }),
                dataIndex: 'palletCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'palletCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.code',
                    defaultMessage: '箱单号'
                }),
                dataIndex: 'code',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.length',
                    defaultMessage: '长度(mm)'
                }),
                dataIndex: 'length',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.width',
                    defaultMessage: '宽度(mm)'
                }),
                dataIndex: 'width',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.height',
                    defaultMessage: '高度(mm)'
                }),
                dataIndex: 'height',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.volume',
                    defaultMessage: '体积(m3)'
                }),
                dataIndex: 'volume',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.netWeight',
                    defaultMessage: '净重(kg)'
                }),
                dataIndex: 'netWeight',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.grossWeight',
                    defaultMessage: '毛重(kg)'
                }),
                dataIndex: 'grossWeight',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.status',
                    defaultMessage: '装箱单状态'
                }),
                dataIndex: 'status',
                render: text => conventEnumValueToString(packingListTypeAdd, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.containerNo',
                    defaultMessage: '柜图号'
                }),
                dataIndex: 'containerNo',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.createTime',
                    defaultMessage: '创建时间'
                }),
                dataIndex: 'createTime',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'createTime' && this.props.sortedOrder,
                render: text => formatDateTime(text, DATE_FORMAT)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.remark',
                    defaultMessage: '备注'
                }),
                dataIndex: 'remark',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partCode',
                    defaultMessage: '备件编号'
                }),
                dataIndex: 'partCode',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partName',
                    defaultMessage: '备件名称'
                }),
                dataIndex: 'partName',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.partNetWeight',
                    defaultMessage: '备件净重'
                }),
                dataIndex: 'partNetWeight',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.oldPartCode',
                    defaultMessage: '被替换备件编号'
                }),
                dataIndex: 'oldPartCode',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.oldPartName',
                    defaultMessage: '被替换备件名称'
                }),
                dataIndex: 'oldPartName',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.currency',
                    defaultMessage: '币种'
                }),
                dataIndex: 'currency',
                render: text => conventEnumValueToString(currency, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.quantity',
                    defaultMessage: '数量'
                }),
                dataIndex: 'quantity',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.price',
                    defaultMessage: '单价'
                }),
                dataIndex: 'price',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.totalAmount',
                    defaultMessage: '总价'
                }),
                dataIndex: 'totalAmount',
                render: text => formatAmount(text)
            }];

        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
